﻿using System;
using System.ComponentModel;
using System.Data;
using System.Linq;
using CDOP.Model;
using CDOP.Utility;
using System.Collections.Generic;

namespace CDOP
{
    public partial class AddMasterDetailsData : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["LoggedUser"] == null)
                    Response.Redirect("Login.aspx");
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    var cdopRef = Convert.ToInt32(Request.QueryString["id"]);
                    var recordToEdit = new MasterTable().GetpatientByCdopRef(cdopRef);
                    Session["RecordToSave"] = recordToEdit;
                    UCBasicInformation.LoadObjectValues(ref recordToEdit);
                    UCPanelReview.LoadObjectValues(ref recordToEdit);
                    UCOtherAgencies.LoadObjectValues(ref recordToEdit);
                    UCMedicalHistory.LoadObjectValues(ref recordToEdit);
                    UCProcessingofDeath.LoadObjectValues(ref recordToEdit);
                    if (!string.IsNullOrEmpty(Request.QueryString["isView"]))
                        if (Request.QueryString["isView"] == "t")
                        {
                            pnlBasicInfo.Enabled = false;
                            pnlPanelReview.Enabled = false;
                            pnlOtherAgencies.Enabled = false;
                            pnlMedicalHistory.Enabled = false;
                            pnlProcessingofDeath.Enabled = false;
                            btnSave.Visible = false;
                            btnExportExcel.Enabled = true;
                        }
                }
                else
                {
                    Session["RecordToSave"] = null;
                }
            }
        }

        //[WebMethod]
        //public string IsCdopExists(string data)
        //{
        //    var isAvailable = UCBasicInformation.IsCdopReferenceAvailable();
        //    return isAvailable ? "True" : "False";
        //} 


        #region Wizard Navigation
        protected void BtnBasicInfoNextClick(object sender, EventArgs e)
        {
            if (Session["RecordToSave"] == null)
            {
                var recordToSave = new MasterTable();
                UCBasicInformation.AddObjectValues(ref recordToSave);
                Session["RecordToSave"] = recordToSave;
            }
            else
            {
                var currentRecord = (MasterTable)Session["RecordToSave"];
                UCBasicInformation.AddObjectValues(ref currentRecord);
            }

            pnlBasicInfo.Visible = false;
            pnlBasicInfoNavigation.Visible = false;
            pnlPanelReview.Visible = true;
            pnlReviewNavigation.Visible = true;

            lnkBtnToBasicInfo.Visible = true;
            lnkBtnToPanelReview.Visible = false;
            lnkBtnToOtherAgencies.Visible = true;
            lnkBtnToMedicalHistory.Visible = true;
            lnkBtnToProcessingofDeath.Visible = true;

            hdnCurrentpanel.Value = "Review";
        }

        protected void BtnPanelReviewPreviousClick(object sender, EventArgs e)
        {
            pnlBasicInfo.Visible = true;
            pnlBasicInfoNavigation.Visible = true;
            pnlPanelReview.Visible = false;
            pnlReviewNavigation.Visible = false;

            lnkBtnToBasicInfo.Visible = false;
            lnkBtnToPanelReview.Visible = true;
            lnkBtnToOtherAgencies.Visible = true;
            lnkBtnToMedicalHistory.Visible = true;
            lnkBtnToProcessingofDeath.Visible = true;

            hdnCurrentpanel.Value = "BasicInfo";
        }

        protected void BtnPanelReviewNextClick(object sender, EventArgs e)
        {
            if (Session["RecordToSave"] == null)
            {
                var recordToSave = new MasterTable();
                UCPanelReview.AddObjectValues(ref recordToSave);
                Session["RecordToSave"] = recordToSave;
            }
            else
            {
                var currentRecord = (MasterTable)Session["RecordToSave"];
                UCPanelReview.AddObjectValues(ref currentRecord);
            }
            pnlPanelReview.Visible = false;
            pnlReviewNavigation.Visible = false;
            pnlOtherAgencies.Visible = true;
            pnlOtherAgenciewNavigation.Visible = true;

            lnkBtnToBasicInfo.Visible = true;
            lnkBtnToPanelReview.Visible = true;
            lnkBtnToOtherAgencies.Visible = false;
            lnkBtnToMedicalHistory.Visible = true;
            lnkBtnToProcessingofDeath.Visible = true;

            hdnCurrentpanel.Value = "OtherAgencies";
        }

        protected void BtnOtherAgenciesPreviousClick(object sender, EventArgs e)
        {
            pnlPanelReview.Visible = true;
            pnlReviewNavigation.Visible = true;
            pnlOtherAgencies.Visible = false;
            pnlOtherAgenciewNavigation.Visible = false;

            lnkBtnToBasicInfo.Visible = true;
            lnkBtnToPanelReview.Visible = false;
            lnkBtnToOtherAgencies.Visible = true;
            lnkBtnToMedicalHistory.Visible = true;
            lnkBtnToProcessingofDeath.Visible = true;

            hdnCurrentpanel.Value = "Review";
        }

        protected void BtnOtherAgenciesNextClick(object sender, EventArgs e)
        {
            if (Session["RecordToSave"] == null)
            {
                var recordToSave = new MasterTable();
                UCOtherAgencies.AddObjectValues(ref recordToSave);
                Session["RecordToSave"] = recordToSave;
            }
            else
            {
                var currentRecord = (MasterTable)Session["RecordToSave"];
                UCOtherAgencies.AddObjectValues(ref currentRecord);
            }
            pnlOtherAgencies.Visible = false;
            pnlOtherAgenciewNavigation.Visible = false;
            pnlMedicalHistory.Visible = true;
            pnlMedicalHistoryNavigation.Visible = true;

            lnkBtnToBasicInfo.Visible = true;
            lnkBtnToPanelReview.Visible = true;
            lnkBtnToOtherAgencies.Visible = true;
            lnkBtnToMedicalHistory.Visible = false;
            lnkBtnToProcessingofDeath.Visible = true;

            hdnCurrentpanel.Value = "MedicalHistory";
        }

        protected void BtnMedicalHistoryPreviousClick(object sender, EventArgs e)
        {
            pnlOtherAgencies.Visible = true;
            pnlOtherAgenciewNavigation.Visible = true;
            pnlMedicalHistory.Visible = false;
            pnlMedicalHistoryNavigation.Visible = false;

            lnkBtnToBasicInfo.Visible = true;
            lnkBtnToPanelReview.Visible = true;
            lnkBtnToOtherAgencies.Visible = false;
            lnkBtnToMedicalHistory.Visible = true;
            lnkBtnToProcessingofDeath.Visible = true;

            hdnCurrentpanel.Value = "OtherAgencies";
        }

        protected void BtnMedicalHistoryNextClick(object sender, EventArgs e)
        {
            if (Session["RecordToSave"] == null)
            {
                var recordToSave = new MasterTable();
                UCMedicalHistory.AddObjectValues(ref recordToSave);
                Session["RecordToSave"] = recordToSave;
            }
            else
            {
                var currentRecord = (MasterTable)Session["RecordToSave"];
                UCMedicalHistory.AddObjectValues(ref currentRecord);
            }
            pnlMedicalHistory.Visible = false;
            pnlMedicalHistoryNavigation.Visible = false;
            pnlProcessingofDeath.Visible = true;
            pnlProcessingDeathNavigation.Visible = true;

            lnkBtnToBasicInfo.Visible = true;
            lnkBtnToPanelReview.Visible = true;
            lnkBtnToOtherAgencies.Visible = true;
            lnkBtnToMedicalHistory.Visible = true;
            lnkBtnToProcessingofDeath.Visible = false;

            hdnCurrentpanel.Value = "DeathProcessing";
        }

        protected void BtnProcessingofDeathPreviousClick(object sender, EventArgs e)
        {
            if (Session["RecordToSave"] == null)
            {
                var recordToSave = new MasterTable();
                UCProcessingofDeath.AddObjectValues(ref recordToSave);
                Session["RecordToSave"] = recordToSave;
            }
            else
            {
                var currentRecord = (MasterTable)Session["RecordToSave"];
                UCProcessingofDeath.AddObjectValues(ref currentRecord);
            }
            pnlMedicalHistory.Visible = true;
            pnlMedicalHistoryNavigation.Visible = true;
            pnlProcessingofDeath.Visible = false;
            pnlProcessingDeathNavigation.Visible = false;

            lnkBtnToBasicInfo.Visible = true;
            lnkBtnToPanelReview.Visible = true;
            lnkBtnToOtherAgencies.Visible = true;
            lnkBtnToMedicalHistory.Visible = false;
            lnkBtnToProcessingofDeath.Visible = true;

            hdnCurrentpanel.Value = "MedicalHistory";
        }
        #endregion

        protected void BtnSaveClick(object sender, EventArgs e)
        {
            MasterTable recordToSave;
            CallSaveForPanleNavigation();
            recordToSave = (MasterTable)Session["RecordToSave"];
            var db = new MasterTable();
            var isSuccesful = db.SaveMasterRecord(recordToSave);
            lblMessage.Text = isSuccesful ? "Record added successfully" : "Saving failed for the record";
        }

        protected void LnkBtnToBasicInfoClick(object sender, EventArgs e)
        {
            CallSaveForPanleNavigation();

            pnlBasicInfo.Visible = true;
            pnlPanelReview.Visible = false;
            pnlOtherAgencies.Visible = false;
            pnlMedicalHistory.Visible = false;
            pnlProcessingofDeath.Visible = false;

            pnlBasicInfoNavigation.Visible = true;
            pnlReviewNavigation.Visible = false;
            pnlOtherAgenciewNavigation.Visible = false;
            pnlMedicalHistoryNavigation.Visible = false;
            pnlProcessingDeathNavigation.Visible = false;

            lnkBtnToBasicInfo.Visible = false;
            lnkBtnToPanelReview.Visible = true;
            lnkBtnToOtherAgencies.Visible = true;
            lnkBtnToMedicalHistory.Visible = true;
            lnkBtnToProcessingofDeath.Visible = true;

            hdnCurrentpanel.Value = "BasicInfo";
        }

        protected void LnkBtnToPanelReviewClick(object sender, EventArgs e)
        {
            CallSaveForPanleNavigation();

            pnlBasicInfo.Visible = false;
            pnlPanelReview.Visible = true;
            pnlOtherAgencies.Visible = false;
            pnlMedicalHistory.Visible = false;
            pnlProcessingofDeath.Visible = false;

            pnlBasicInfoNavigation.Visible = false;
            pnlReviewNavigation.Visible = true;
            pnlOtherAgenciewNavigation.Visible = false;
            pnlMedicalHistoryNavigation.Visible = false;
            pnlProcessingDeathNavigation.Visible = false;

            lnkBtnToBasicInfo.Visible = true;
            lnkBtnToPanelReview.Visible = false;
            lnkBtnToOtherAgencies.Visible = true;
            lnkBtnToMedicalHistory.Visible = true;
            lnkBtnToProcessingofDeath.Visible = true;

            hdnCurrentpanel.Value = "Review";
        }

        protected void LnkBtnToOtherAgenciesClick(object sender, EventArgs e)
        {
            CallSaveForPanleNavigation();

            pnlBasicInfo.Visible = false;
            pnlPanelReview.Visible = false;
            pnlOtherAgencies.Visible = true;
            pnlMedicalHistory.Visible = false;
            pnlProcessingofDeath.Visible = false;

            pnlBasicInfoNavigation.Visible = false;
            pnlReviewNavigation.Visible = false;
            pnlOtherAgenciewNavigation.Visible = true;
            pnlMedicalHistoryNavigation.Visible = false;
            pnlProcessingDeathNavigation.Visible = false;

            lnkBtnToBasicInfo.Visible = true;
            lnkBtnToPanelReview.Visible = true;
            lnkBtnToOtherAgencies.Visible = false;
            lnkBtnToMedicalHistory.Visible = true;
            lnkBtnToProcessingofDeath.Visible = true;

            hdnCurrentpanel.Value = "OtherAgencies";
        }

        protected void LnkBtnToMedicalHistoryClick(object sender, EventArgs e)
        {
            CallSaveForPanleNavigation();

            pnlBasicInfo.Visible = false;
            pnlPanelReview.Visible = false;
            pnlOtherAgencies.Visible = false;
            pnlMedicalHistory.Visible = true;
            pnlProcessingofDeath.Visible = false;

            pnlBasicInfoNavigation.Visible = false;
            pnlReviewNavigation.Visible = false;
            pnlOtherAgenciewNavigation.Visible = false;
            pnlMedicalHistoryNavigation.Visible = true;
            pnlProcessingDeathNavigation.Visible = false;

            lnkBtnToBasicInfo.Visible = true;
            lnkBtnToPanelReview.Visible = true;
            lnkBtnToOtherAgencies.Visible = true;
            lnkBtnToMedicalHistory.Visible = false;
            lnkBtnToProcessingofDeath.Visible = true;

            hdnCurrentpanel.Value = "MedicalHistory";
        }

        protected void LnkBtnToProcessingofDeathClick(object sender, EventArgs e)
        {
            CallSaveForPanleNavigation();

            pnlBasicInfo.Visible = false;
            pnlPanelReview.Visible = false;
            pnlOtherAgencies.Visible = false;
            pnlMedicalHistory.Visible = false;
            pnlProcessingofDeath.Visible = true;

            pnlBasicInfoNavigation.Visible = false;
            pnlReviewNavigation.Visible = false;
            pnlOtherAgenciewNavigation.Visible = false;
            pnlMedicalHistoryNavigation.Visible = false;
            pnlProcessingDeathNavigation.Visible = true;

            lnkBtnToBasicInfo.Visible = true;
            lnkBtnToPanelReview.Visible = true;
            lnkBtnToOtherAgencies.Visible = true;
            lnkBtnToMedicalHistory.Visible = true;
            lnkBtnToProcessingofDeath.Visible = false;

            hdnCurrentpanel.Value = "DeathProcessing";
        }

        private void CallSaveForPanleNavigation()
        {
            var sender = this;
            var eventArgs = new EventArgs();

            MasterTable recordToSave;
            if (Session["RecordToSave"] == null)
                recordToSave = new MasterTable();
            else
                recordToSave = (MasterTable)Session["RecordToSave"];

            switch (hdnCurrentpanel.Value)
            {
                case "BasicInfo":
                    UCBasicInformation.AddObjectValues(ref recordToSave);
                    break;
                case "Review":
                    UCPanelReview.AddObjectValues(ref recordToSave);
                    break;
                case "OtherAgencies":
                    UCOtherAgencies.AddObjectValues(ref recordToSave);
                    break;
                case "MedicalHistory":
                    UCMedicalHistory.AddObjectValues(ref recordToSave);
                    break;
                case "DeathProcessing":
                    UCProcessingofDeath.AddObjectValues(ref recordToSave);
                    break;
                default:
                    break;
            }

            if (Session["RecordToSave"] == null)
                Session["RecordToSave"] = recordToSave;
        }

        protected void BtnExportExcelClick(object sender, EventArgs e)
        {
            var record = Session["RecordToSave"] as MasterTable;
            if (record == null) return;
            var properties = Helper.GetRelevantProperties(TypeDescriptor.GetProperties(typeof(MasterTable)), "AddMasterDetailsData");
            var data = (from PropertyDescriptor property in properties
                        let val = property.GetValue(record) == null ? string.Empty : property.GetValue(record).ToString()
                        select new KeyValuePair<string, string>(property.Name, val)).ToList();
            ExportToExcel(data);
        }

        public void ExportToExcel(List<KeyValuePair<string, string>> data)
        {
            var table = Helper.ConvertTo(data);
            const string attachment = "attachment; filename=ExportedData.xls";
            Response.ClearContent();
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/vnd.ms-excel";
            var tab = string.Empty;
            foreach (DataColumn dc in table.Columns)
            {
                Response.Write(tab + dc.ColumnName);
                tab = "\t";
            }
            Response.Write("\n");
            foreach (DataRow dr in table.Rows)
            {
                tab = string.Empty;
                int i;
                for (i = 0; i < table.Columns.Count; i++)
                {
                    Response.Write(tab + dr[i]);
                    tab = "\t";
                }
                Response.Write("\n");
            }
            Response.End();
        }

    }

}
